package com.fastwire.carriere.service.validator;

import org.apache.commons.validator.DateValidator;
import org.apache.commons.validator.routines.IntegerValidator;

import com.fastwire.carriere.common.Messages;
import com.fastwire.carriere.common.exception.InputValidationException;
import com.fastwire.carriere.common.util.DateUtil;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.pegasus.vo.PegasusActivityDetailsVo;
import com.fastwire.carriere.pegasus.vo.PegasusNPPInfoVo;
import com.fastwire.carriere.pegasus.vo.PegasusOrderDetailsVo;

// TODO: Auto-generated Javadoc
/**
 * The Class PegasusOrderInputValidator.
 */
public class PegasusOrderInputValidator {

	/**
	 * Validate service order.
	 * 
	 * @param orderDetailsVo
	 *            the order details vo
	 * @throws InputValidationException
	 *             the input validation exception
	 */
	public static void validateServiceOrder(PegasusOrderDetailsVo orderDetailsVo)
			throws InputValidationException {
		if (StringUtil.isEmptyString(orderDetailsVo.getWorkOrderNo())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "workOrderNo" }));
		}

		if (StringUtil.isEmptyString(orderDetailsVo.getServiceId())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Service Id" }));
		}

		if (orderDetailsVo.getCustomerReqDate() == null) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Customer Required Date" }));

		}

		DateValidator dateValidator = DateValidator.getInstance();
		if (!dateValidator.isValid(orderDetailsVo.getCustomerReqDate(),
				DateUtil.DATE_SLASH, false)) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.dateFormat.invalid",
					new String[] { "Customer Required Date" }));
		}

		if (StringUtil.isEmptyString(orderDetailsVo.getOrderActionType())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Order Action Type" }));
		}

		if (StringUtil.isEmptyString(orderDetailsVo.getWorkOrderType())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Work Order Type" }));
		}

		if (StringUtil.isEmptyString(orderDetailsVo.getServiceActionType())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Service Action Type" }));
		}

		PegasusActivityDetailsVo[] activityDetailsVos = orderDetailsVo
				.getActivities();
		if (activityDetailsVos != null) {

			for (int index = 0; index < activityDetailsVos.length; index++) {
				validateServiceOrderActivity(activityDetailsVos[index]);
			}
		}

		PegasusNPPInfoVo[] infoVos = orderDetailsVo.getProductInfos();
		if (infoVos != null) {
			for (int index = 0; index < infoVos.length; index++) {
				validateProductInfo(infoVos[index]);
			}
		}

	}

	/**
	 * Validate service order activity.
	 * 
	 * @param activityDetailsVo
	 *            the activity details vo
	 * @throws InputValidationException
	 *             the input validation exception
	 */
	public static void validateServiceOrderActivity(
			PegasusActivityDetailsVo activityDetailsVo)
			throws InputValidationException {

		IntegerValidator validator = IntegerValidator.getInstance();

		if (!validator.isValid(activityDetailsVo.getActivityNo())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.numeric.invalid",
					new String[] { "Activity No" }));
		}

		if (StringUtil.isEmptyString(activityDetailsVo.getGroupId())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Group Id" }));
		}

		if (StringUtil.isEmptyString(activityDetailsVo.getActivityName())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Activity Name" }));
		}

		if (StringUtil.isEmptyString(activityDetailsVo.getActivityStatus())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Activity Status" }));
		}

	}

	/**
	 * Validate product info.
	 * 
	 * @param productInfo
	 *            the product info
	 * @throws InputValidationException
	 *             the input validation exception
	 */
	public static void validateProductInfo(PegasusNPPInfoVo productInfo)
			throws InputValidationException {

		if (StringUtil.isEmptyString(productInfo.getParaName())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Parameter Name" }));
		}

		if (StringUtil.isEmptyString(productInfo.getParaValue())) {
			throw new InputValidationException(Messages.getString(
					"message.common.error.nullempty",
					new String[] { "Parameter Value" }));
		}

		IntegerValidator validator = IntegerValidator.getInstance();
		Integer actionNo = validator.validate(productInfo.getProductAction());

		if (actionNo != null) {
			if (!validator.isInRange(actionNo, 1, 3)) {
				throw new InputValidationException(Messages.getString(
						"message.common.error.nullempty", new String[] {
								"Product Info Action No", "1", "3" }));
			}

		} else {
			throw new InputValidationException(Messages.getString(
					"message.common.error.numeric.invalid",
					new String[] { "Product Info Action No" }));
		}

	}
}
